﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CommonMvc.Models;
using CommonMvc.Models.Home;
using CommonMvc.Models.Concreate;
using CommonMvc.Common;
using Microsoft.Extensions.Caching.Distributed;
using CommonMvc.Models.CommonModels;
using Newtonsoft.Json;
using CommonMvc.Filters;

namespace CommonMvc.Controllers
{
    [TypeFilter(typeof(BaseAuthorizationFilter))]
    public class HomeController : BaseController
    {
        public HomeController(DataContext context , IWorkContext workContext, IDistributedCache distributedCache )
           :base(context, workContext, distributedCache)//依赖注入得到实例  
         { }



        [TypeFilter(typeof(BaseAuthorizationFilter))]
        public IActionResult Index()
        {
            return View();
        }
        [TypeFilter(typeof(BaseAuthorizationFilter))]
        public IActionResult Create()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";
            

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public IActionResult Login(HomeLogin LoginModel) {
          
           //要判断用户是否是WeiXin平台下的
            Person p =  _dataBaseContext.Persons.ToList().Where(person => person.Name == LoginModel.Name).FirstOrDefault() as Person;
            if (p == null) {
                return JsonFailResult(null, "用户不存在,或账户密码错误");
            }
            User user = new User
            {
                UserId = p.Id,
                UserType = UserType.WeiXin,
                IsLogin = true
            };
            String a = _cookieManager.GetCookieValue("WeiXin");
            User user2 = JsonConvert.DeserializeObject<User>(a);

            BaseLogin(user);
            return JsonSuccessResult(null, "登录成功");
        }

       


    }

    
}
